package com.zhang.controller;


import com.zhang.domain.Employee;
import com.zhang.domain.PermissionVO;
import com.zhang.domain.Permissions;
import com.zhang.domain.Role;
import com.zhang.query.RoleQuery;
import com.zhang.service.IPermissionService;
import com.zhang.service.IRoleService;
import com.zhang.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-14 9:06
 */

@RestController
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService service;
    @GetMapping
    public AjaxResult permissions(){
        try {
            List<Permissions> permission = service.findPermission();
            return AjaxResult.me(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping("/{roleId}")
    public AjaxResult findMyPermission(@PathVariable("roleId") Long roleId){
        try {
            List<Integer> myPermission = service.findMyPermission(roleId);
            return AjaxResult.me(myPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PutMapping
    public AjaxResult addPermission(@RequestBody PermissionVO pv){
        try {
            Long roleId = pv.getRoleId();
            Long[] permissionIds = pv.getPermissionIds();
            service.addRp(roleId,permissionIds);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"添加失败");
        }
    }
    @PostMapping
    public AjaxResult getRoId(Employee employee){
        try {
            Long roleId = service.findRoIdById(employee.getId());
            return AjaxResult.me(roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
}
